package academic.ui.client.widgets;

import ui.client.GenericClickable;
import ui.client.GenericContainerTag;
import ui.client.widgets.TextAreaInput;

import academic.ui.client.util.ClientHelper;

import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Image;

import ecommerce.soco.shared.GWT_Discussion;
import ecommerce.soco.shared.GWT_Message;

public class DiscussionWidget extends Composite {

	private static String DEFAULT_AREA_TEXT = "type your question/comment...";
	GenericContainerTag div = new GenericContainerTag("div");
	private Anchor loginLink = new Anchor("Login To Ask A Question");
	private TextAreaInput textAreaMessage = new TextAreaInput("","comment",DEFAULT_AREA_TEXT);
	private CheckBox emailUpdateBox = new CheckBox("email me when new messages are posted");
	private GenericClickable submitMessageButton = new GenericClickable("a");
	private GenericContainerTag msgSection = new GenericContainerTag("div");
	
	public DiscussionWidget() {
		div.clear();
		GenericContainerTag heading = new GenericContainerTag("h3","have a question?");
		div.add(heading);
		div.add(loginLink);
		loginLink.getElement().setId("loginLink");
		loginLink.getElement().setAttribute("rel", "#signInModal");
		GenericContainerTag submitSpan = new GenericContainerTag("span","post message");
		submitSpan.setStyleName("submitButton");
		submitMessageButton.setStyleName("submitButtonLink");
		submitMessageButton.add(submitSpan);
		div.add(textAreaMessage);
		div.add(emailUpdateBox);
		div.add(submitMessageButton);
		textAreaMessage.getField().addFocusHandler(new FocusHandler() {

			@Override
			public void onFocus(FocusEvent event) {
				if(DEFAULT_AREA_TEXT.equals(textAreaMessage.getText())) {
					textAreaMessage.getField().setText("");
					
				}
					
			}
			
		});
		
		textAreaMessage.getField().addKeyUpHandler(new KeyUpHandler() {

			@Override
			public void onKeyUp(KeyUpEvent event) {
				if("".equals(textAreaMessage.getText()))
					submitMessageButton.addStyleName("inactive");
				else
					submitMessageButton.removeStyleName("inactive");
			}
			
		});
		
		div.add(msgSection);
		initWidget(div);
	}
	
	public void setupMessages(GWT_Discussion discussion,Boolean isLoggedIn)
	{
		msgSection.clear();
		textAreaMessage.getField().setText(DEFAULT_AREA_TEXT);

		if(!isLoggedIn)
		{
			loginLink.setVisible(true);
			textAreaMessage.getField().setEnabled(false);
			emailUpdateBox.setEnabled(false);
			submitMessageButton.addStyleName("inactive");
		}
		else
		{
			loginLink.setVisible(false);
			textAreaMessage.getField().setEnabled(true);
			emailUpdateBox.setEnabled(true);
		}
		
		if(discussion!=null)
		{
			for(GWT_Message message : discussion.getMessages())
			{
				addMessage(message);
			}
		}
		
	}
	
	
	public HasClickHandlers showLoginView() {
		return loginLink;
	}
	
	public HasClickHandlers submitMessageButton() {
		return submitMessageButton;
	}
	
	public HasValue<Boolean> emailUpdateValue() {
		return emailUpdateBox;
	}
	
	public HasText textAreaMessage() {
		return textAreaMessage;
	}

	public void addMessage(GWT_Message message) {
		GenericContainerTag anotherMsg = new GenericContainerTag("div");
		anotherMsg.setStyleName("aMessage");
		msgSection.insert(anotherMsg,0);
		Image userImg = new Image(ClientHelper.getCustomerPhotoURL(message.getCustomer()));
		Double ratio = message.getCustomer().getPhotoRatio()!=null &&message.getCustomer().getPhotoRatio() >0?message.getCustomer().getPhotoRatio():1;
		userImg.setSize(String.valueOf(100/ratio)+"px", "100px");
		userImg.setStyleName("userDiscussionImg");
		anotherMsg.add(userImg);
		anotherMsg.add(new GenericContainerTag("div",message.getCustomer().getFirstName()+" "+message.getCustomer().getLastName()));
		anotherMsg.add(new GenericContainerTag("div",ClientHelper.formatDateTime(message.getUpdateDate())));
		GenericContainerTag messagePTag = new GenericContainerTag("p",message.getMessage());
		messagePTag.setStyleName("discussionMsg");
		anotherMsg.add(messagePTag);
	}

	public void resetText() {
		textAreaMessage.setText(DEFAULT_AREA_TEXT);
	}
}
